const Router = require('koa-router');
const router = new Router();
const {db, Op, QueryTypes} = require("../../models/db");

// 常用菜单保存
router.post('/common/saveUsedMenu', async ctx => {
    let body = ctx.request.body
    let user = ctx.user
    let menuId = body.id;
    let adminId = user.id;
    let tenantId = user.tenantId;
    let menuUsed = await db.query(`select id,times from menu_used where menuId=${menuId} and adminId=${adminId}`, {type: QueryTypes.SELECT})
    let times = 1;
    try {
        if (menuUsed.length === 0) {
            await db.models['menuUsed'].create({
                menuId,
                tenantId,
                times,
                adminId
            })
        }
        else {
            let data = menuUsed[0]
            times = data.times + 1
            let model = await db.models['menuUsed'].findByPk(data.id)
            model.times = times
            await model.save()
        }
        return ctx.suc('')
    } catch (e) {
        return ctx.err(e);
    }
})





module.exports = router;